<?php

require_once('../util/DataBaseUtil.php');
require_once('../util/LogUtil.php');

require_once('../vo/Group.php');
require_once('../vo/User.php');
require_once('../vo/request/SaveGroupRequest.php');
require_once('../vo/response/SaveGroupResponse.php');

/***************************/
/** FIND GROUPS BY USER **/
/***************************/

function findGroupsByUser($request)
{
  $response = new FindGroupsByUserResponse();

  connect();

  $userGroups = array();
  $sql = "SELECT * FROM `groups` WHERE `userId` = ".$request['userId']." ORDER BY `lastName` ASC, firstName ASC;";
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving groups.";
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $g = new Group();
    $g->init($r['id'], $r['userId'], $r['name']);
    $userGroups[] = $g;
  }

  mysql_close();

  $response->userGroups = $userGroups;
  return $response;
}

/******************/
/** CREATE GROUP **/
/******************/

function saveGroup($request)
{
  $response = new SaveGroupResponse();
  $dataGroup = $request['group'];

  $group = new Group();
  $group->id = $dataGroup['id'];
  $group->userId = $dataGroup['userId'];
  $group->name = $dataGroup['name'];

  connect();

  log_info("group.id = ".$group->id
          ."\nuserId = ".$request['userId']);

  $query = "INSERT INTO `groups` (userId, name) "
            ."VALUES ('".$request['userId']."', '".$group->name."');";

  log_info($query);
  mysql_query($query)
    or die ('Une erreur est survenue lors de l\'ajout.<br>'.mysql_error());

  $userGroups = array();
  $sql = "SELECT * FROM `groups` WHERE `userId` = ".$request['userId']." ORDER BY `name` ASC;";
  log_info("select final = ".$sql);
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving groups.";
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $g = new Group();
    $g->init($r['id'], $r['userId'], $r['name']);
    $userGroups[] = $g;
  }

  mysql_close();

  $response->userGroups = $userGroups;
  return $response;
}

/********************/
/** DELETE GROUP **/
/********************/

function deleteGroup($request)
{
  $response = new DeleteGroupResponse();
  $userId = $request['userId'];
  $groupId = $request['groupId'];

  log_info("group.id = ".$groupId
          ."\nuserId = ".$userId);

  connect();

  $query = "DELETE FROM `groups` WHERE `id`='".$groupId."';";
  log_info($query);
  mysql_query($query);

  $userGroups = array();
  $sql = "SELECT * FROM `groups` WHERE `userId` = ".$userId." ORDER BY `name` ASC;";
  $res = mysql_query($sql);

  if (!isset($res) or ($res == 0))
  {
    $response->errorMessage = "A problem occured when retrieving groups.";
    return $response;
  }

  while ($r = mysql_fetch_array($res))
  {
    $g = new Group();
    $g->init($r['id'], $r['userId'], $r['name']);
    $userGroups[] = $g;
  }

  mysql_close();

  $response->userGroups = $userGroups;
  return $response;
}

?>
